package com.mycompany.biblio.controller;

import com.mycompany.biblio.business.SecurityService;
import com.mycompany.biblio.business.StatusDAO;
import com.mycompany.biblio.model.Status;
import java.util.Arrays;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import java.util.Date;
import java.util.List;
import javax.faces.model.ListDataModel;

@ManagedBean
@SessionScoped
public class StatusController
{

    // ======================================
    // =             Attributes             =
    // ======================================

    @EJB
    private StatusDAO statusDAO;
    
    @EJB
    private SecurityService securityService;
    
    private Status status = new Status();
    private ListDataModel<Status> statusList;
    private String statusVision;

    public String getStatusVision() {
        return statusVision;
    }

    public void setStatusVision(String statusVision) {
        this.statusVision = statusVision;
    }
    private final List<String> statusVisionList = Arrays.asList("Public", "Friends", "Private");

    public List<String> getStatusVisionList() {
        return statusVisionList;
    }

    public ListDataModel<Status> getStatusList() {
        updateStatusList();
        return statusList;
    }

    public void setStatusList(ListDataModel<Status> statusList) {
        this.statusList = statusList;
    }
    
    private void updateStatusList()
    {
        statusList = new ListDataModel(statusDAO.getAllStatuses());
    }

    // ======================================
    // =           Public Methods           =
    // ======================================
    
    public boolean hasLike(Status status)
    {
        //check if current user has like this status before        
        return statusDAO.hasLike(securityService.getCurrentUser(), status);
    }
    
    public String addComment(Status status)
    {                        
        statusDAO.addComment(status, status.getNewComment().getValue());        
        return null;
    }
    
    public String likeAStatus(Status status)
    {
        statusDAO.likeStatus(securityService.getCurrentUser(), status);
        return null;
    }
    
        
    public String unlikeAStatus(Status status)
    {
        statusDAO.unlikeStatus(securityService.getCurrentUser(), status);
        return null;
    }
    
    public void doNew() 
    {
        status = new Status();        
    }

    public String doCreate()
    {
        switch (statusVision)
        {
            case "Public":
                status.setVision(Status.PUBLIC);
                break;
            case "Friends":
                status.setVision(Status.FRIEND_ONLY);
                break;
                case "Private":
                status.setVision(Status.PRIVATE);
                break;
        }
        status.setDatePublication(new Date());
        status.setProprietaire(securityService.getCurrentUser());
        status = statusDAO.create(status);
        doNew();
        return null;
    }
    
    // ======================================
    // =          Getters & Setters         =
    // ======================================

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    
}